<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="kr.co.insoft.framework.config.Authorities" %>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<div class="page-header custom-page-header">
	<h1><spring:message code="menu.template.application"/></h1>
	<small><spring:message code="menu.template.application.desc"/></small>
</div>
<form:form modelAttribute="applicationEntityModel" role="form" cssClass="form-horizontal">
	<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
	<form:input path="applicationid" type="hidden"/>
	<form:input path="companyid" type="hidden"/>
	<!-- 어플리케이션 네임 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.application.name"/></label>
		<div class="col-sm-10">
			<p class="form-control-static">${applicationEntityModel.applicationName }</p>
		</div>
	</div>
	<!-- 어플리케이션 타입  -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.application.type.name"/></label>
		<div class="col-sm-10">
			<p class="form-control-static">${applicationEntityModel.applicationTypeName }</p>
		</div>
	</div>
	<!-- 어플리케이션 파일 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.application.file"/></label>
		<div class="col-sm-10 text-right ">
			<button type="button" class="btn btn-default btnCreate" data-types="file" ><spring:message code="button.add"/></button>
		</div>
		<div class="col-sm-offset-2 col-sm-10 topmargin20"></div>
		<div class="col-sm-offset-2 col-sm-10">
			<table class="table table-hover topmargin20">
				<thead>
					<tr>
						<th>File Name</th>
						<th>Version</th>
						<th><spring:message code="common.updatedDate"/></th>
						<th></th>
					</tr>
				</thead>
				<tbody class="fileGroup">
				</tbody>
			</table>
		</div>
	</div>
	<!-- 이벤트 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.event"/></label>
		<div class="col-sm-10 text-right ">
			<button type="button" class="btn btn-default btnCreate" data-types="event" ><spring:message code="button.add"/></button>
		</div>
		<div class="eventGroup col-sm-offset-2 col-sm-10 topmargin20">
		</div>
	</div>
	<!-- 파라메터 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.param"/></label>
		<div class="col-sm-10 text-right">
			<button type="button" class="btn btn-default btnCreate" data-types="param">
				<spring:message code="button.add"/>
			</button>
		</div>
		<div class="col-sm-offset-2 col-sm-10">
			<table class="table table-hover topmargin20">
				<thead>
					<tr>
						<th style="display:none;"></th>
						<th><spring:message code="template.param.name"/></th>
						<th><spring:message code="template.param.value"/></th>
						<th></th>
					</tr>
				</thead>
				<tbody class="paramGroup">
				</tbody>
			</table>
		</div>
	</div>
	<!-- 요약 설명 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="common.description"/></label>
		<div class="col-sm-10">
			<p class="form-control-static">${applicationEntityModel.applicationDesc }</p>
		</div>
	</div>
	<!--  수정일자 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="common.updatedDate"/></label>
		<div class="col-sm-10">
			<p class="form-control-static">${applicationEntityModel.updatedDate }</p>
		</div>
	</div>
	<!--  하단 버튼 -->
	<div class="form-group">
		<div class="col-sm-12 text-right">
			<button class="btn btn-default btn_link" data-link="./lists" type="button"><spring:message code="button.list"/></button>
			<c:if test="${fn:contains(buttons , Authorities.REMOVE)}">
				<button class="btn btn-primary btnDeleteApp" type="button"><spring:message code="button.delete"/></button>
			</c:if>
		</div>
	</div>
</form:form>
<!--  오른쪽 폼  -->
<div class="content col-sm-6"></div>

<script type="text/javascript">
$(document).ready(function() {
	
	/* 어플리케이션의 파라메터 이벤트를 로드 */
	loadEventList();
	loadParamList();
	
	/* 생성 From */
	$(document).on("click", ".btnCreate", function() {
		var types = $(this).data("types");
		$(".content").removeClass("hidden");
		callCreateForm( "${CONTEXT }/template/"+ types +"/createForm" , $("#applicationid").val() );
	})
	/* 수정 Form */
	.on("click", ".btnEdit", function() {
		$(".content").removeClass("hidden");
        var target = $(this).parent().parent().parent();
		var targetid = target.find("#targetid").val();
		var types = $(this).data("types");
		callEditForm( "${CONTEXT }/template/"+ types +"/edit" , targetid );
	})
	/* 스크립트 변경 */
	.on("change", "#scriptid", function() {
		bootbox.confirm("<spring:message code='template.script.change'/>", function(result) {
			if ( result ) {
				Loading.show();
				var param = {"ip" : "Y", "scriptid": $("#scriptid").val()};
				$.get("${CONTEXT}/template/script/view", param , function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						$(".scriptGroup").html(res);
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 중복 네임 체크 */
	.on("click", ".btnCheckName", function() {
		Loading.show();
		var url = "";
		var target = "";
		var name = "";
		switch($(this).data("types")) {
			case "event":
				url = "${CONTEXT}/template/event/isExistEventName";
				target = $("#eventEntityModel").serialize();
				name = $("#eventName").val();
				break;
			case "param":
				url = "${CONTEXT}/template/param/isExistParamName";
				target = $("#paramEntityModel").serialize();
				name = $("#paramName").val();
				break;
			default:
				break;
		}
		/* 임시 이름같은 경우 중복 체크하지 않음 */
		if( $("#orignalName").val()==name) {
			bootbox.alert("<spring:message code='common.name.available'/>");
			Loading.hide();
			return;
		}
		$.post( url, target , function(res) {
		}).fail(function() {
			bootbox.alert("<spring:message code='common.server.fail'/>");
		}).done(function(res) {
			if (res) {
				bootbox.alert("<spring:message code='common.name.unavailable'/>");
			} else {
				bootbox.alert("<spring:message code='common.name.available'/>", function() {
					$("input[id='chkName']").val("Y");
				});
			}
		}).always(function() {
			Loading.hide();
		});
	})
	/* 취소 */
	.on("click", ".btnCancel", function() {
		$("form[id='applicationEntityModel']").removeClass("col-sm-6").addClass("col-sm-12");
		$(".content").addClass("hidden");
	})
	/* 리스트 삭제 */
	.on("click", ".btnDelete", function() {
		var types = $(this).data("types");
        var target = $(this).parent().parent().parent();
		var targetid = target.find("#targetid").val();
    	var param = '';    
		switch(types) {
			case "event":
				param = {"eventid" : targetid };
				break;
			case "param":
				param = {"paramid" : targetid };
				break;
			default:
				break;
		}
		bootbox.confirm("<spring:message code='common.remove.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				$.post("${CONTEXT }/template/"+ types +"/remove.do", param , function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.remove.ok'/>", function() {
							loadEventList();
							loadParamList();	
						});
					} else {
						bootbox.alert("<spring:message code='common.remove.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 어플리케이션 삭제 */
	.on("click", ".btnDeleteApp", function() {
		bootbox.confirm("<spring:message code='common.remove.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				$.post("./remove", {"applicationid" : $("#applicationid").val() }, function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.remove.ok'/>", function() {
							Loading.link("./lists");							
						});
					} else {
						bootbox.alert("<spring:message code='common.remove.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 생성 Submit */
	.on("click", ".btnCreateSubmit", function() {
		var types = $(this).data("types");
		var target = "";
		switch(types) {
			case "event":
				target = $("#eventEntityModel").serialize();
				break;
			case "param":
				target = $("#paramEntityModel").serialize();
				break;
			default:
				break;
		}
		bootbox.confirm("<spring:message code='common.save.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				$.post("${CONTEXT}/template/"+ types +"/createForm", target , function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.save.ok'/>", function() {
							$("form[id='applicationEntityModel']").removeClass("col-sm-6").addClass("col-sm-12");
							$(".content").addClass("hidden");
							loadEventList();
							loadParamList();	
						});
					} else {
						bootbox.alert("<spring:message code='common.save.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 수정 Submit */
	.on("click", ".btnUpdateSubmit", function() {
		if($("#chkName").val() == 'N'){
			bootbox.alert("<spring:message code='common.name.unavailable'/>");
			return;
		}
		var types = $(this).data("types");
		var target = "";
		switch(types) {
			case "event":
				target = $("#eventEntityModel").serialize();
				break;
			case "param":
				target = $("#paramEntityModel").serialize();
				break;
			default:
				break;
		}
		bootbox.confirm("<spring:message code='common.update.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				$.post("${CONTEXT}/template/"+types+"/edit", target , function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.update.ok'/>", function() {
							$("form[id='applicationEntityModel']").removeClass("col-sm-6").addClass("col-sm-12");
							$(".content").addClass("hidden");
							loadEventList();
							loadParamList();	
						});
					} else {
						bootbox.alert("<spring:message code='common.update.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	});
});

/* 이벤트 로딩 */
function loadEventList() {
	var param = {"applicationid" : '${applicationEntityModel.applicationid }' };
	$.get("${CONTEXT }/template/event/list.do", param , function(res) {
	}).fail(function() {
		bootbox.alert("<spring:message code='common.server.fail'/>");
	}).done(function(res) {
		if ( res ) {
           	var html = "";
           	$(".eventGroup").hide();
            $.each(res, function(index, item) {
            	html +=
          			"<div class='panel-group'>" + 
      				"<div class='panel panel-default'>"+
      					"<div class='panel-heading'>" + 
      						"<h4 class='panel-title'>" +
      			                "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion' href='#panel" + index + "'>" + this.eventName + "</a>" + 
      			               	"<input type='hidden' id='targetid' value='" + this.eventid + "'/>" +
      						"</h4>" +
      					"</div>" + 
      					"<div id='panel"+ index + "' class='panel-collapse collapse'>" +
      						"<div class='panel-body text-right'>" +  
      							"<button class='btn btn-default btnEdit' type='button' data-types='event' style='margin-right:5px;'><spring:message code='button.edit'/></button>" + 
      							"<button class='btn btn-default btnDelete' type='button' data-types='event'><spring:message code='button.delete'/></button>" + 
      							"<textarea id='script' name='script' class='form-control topmargin20' rows='15' disabled>" + this.script +"</textarea>" + 
      						"</div>" +
      					"</div>" +
      			    "</div>" + 
          		"</div>";
            });
            $(".eventGroup").html(html);
		} else {
			bootbox.alert("<spring:message code='common.remove.fail'/>");
		}
	}).always(function() {
		  $(".eventGroup").fadeIn('slow');
	});
}
/* 파라메터 로딩 */ 
function loadParamList() {
	$.get("${CONTEXT }/template/param/list.do", {"applicationid" : '${applicationEntityModel.applicationid }' }, function(res) {
	}).fail(function() {
		bootbox.alert("<spring:message code='common.server.fail'/>");
	}).done(function(res) {
		if ( res ) {
           	var html = "";
            $(".paramGroup").hide();
            $.each(res, function(index, item) {
            	html +=
				"<tr>" + 
					"<td style='display:none;'><input type='text' id='targetid' value='" + this.paramid + "'/></td>" + 
					"<td>" +  this.paramName + "</td>" + 
					"<td>" +  this.paramValue + "</td>" + 
					"<td>" + 
						"<div class='text-right'>" + 
							"<button class='btn btn-default btnEdit' data-types='param' type='button' style='margin-right:5px;'><spring:message code='button.edit'/></button>" + 
							"<button class='btn btn-default btnDelete' data-types='param' type='button'><spring:message code='button.delete'/></button>" + 
						"</div>" + 
					"</td>";							
				
            });
            $(".paramGroup").html(html);
		} else {
			bootbox.alert("<spring:message code='common.remove.fail'/>");
		}
	}).always(function() {
		  $(".paramGroup").fadeIn('slow');
	});
};
/* 생성 폼 호출 */
function callCreateForm(url, applicationid) {
	$(".content").hide();
	$("form[id='applicationEntityModel']").removeClass("col-sm-12").addClass("col-sm-6");
	$.get( url, { "applicationid" : applicationid }, function(res) {
		$(".content").html(res);
	}).always(function() {
		$(".content").fadeIn('slow');
	});
}
/* 수정 폼 호출 */ 
function callEditForm(url, targetid) {
	$(".content").hide();
	$("form[id='applicationEntityModel']").removeClass("col-sm-12").addClass("col-sm-6");
	$.get( url, { "targetid" : targetid }, function(res) {
		$(".content").removeClass("hidden").html('').html(res);
	}).always(function() {
		$(".content").fadeIn('slow');
	});
}
</script>

